package org.jeecgframework.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.ZyEquipmentChangeServiceI;
import org.jeecgframework.web.service.ZyInspectionPlanServiceI;
import org.jeecgframework.web.service.ZyInspectionReportServiceI;
import org.jeecgframework.web.system.pojo.base.TSBaseUser;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSUserOrg;
import org.jeecgframework.web.system.pojo.base.ZyCinemaEntity;
import org.jeecgframework.web.system.pojo.base.ZyContactsEntity;
import org.jeecgframework.web.system.pojo.base.ZyEngineerEntity;
import org.jeecgframework.web.system.pojo.base.ZyEquipmentChangeEntity;
import org.jeecgframework.web.system.pojo.base.ZyInspectionPlanEntity;
import org.jeecgframework.web.system.pojo.base.ZyInspectionReportEntity;
import org.jeecgframework.web.system.service.SystemService;

import com.alibaba.fastjson.JSON;

public class CbsIpTaskWebServiceController {
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");
	private static ZyInspectionPlanServiceI zyInspectionPlanService = ApplicationContextUtil.getBean("zyInspectionPlanService");
	private static ZyInspectionReportServiceI zyInspectionReportService = ApplicationContextUtil.getBean("zyInspectionReportService");
	private static ZyEquipmentChangeServiceI zyEquipmentChangeServiceI = ApplicationContextUtil.getBean("zyEquipmentChangeService");
	
	/**
	 * @author xiul
	 * 2016-7-21 下午5:43:52
	 * Description WebService - 天影负责人拒绝巡检任务 
	 * @param ipId 巡检计划id
	 * @param remark 拒绝原因
	 * @param userId 用户表id
	 */
	public String tpRefuseMission(String ipId,String remark,String userId){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(remark)){
				ZyInspectionPlanEntity zyIp = systemService.getEntity(ZyInspectionPlanEntity.class, ipId);
				if(zyInspectionPlanService.judgeForBatchOperateMission(zyIp,aj,userId)){
					zyInspectionPlanService.doBatchRefuseMission(ipId, remark,"");
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-21 下午5:38:20
	 * Description WebService - 天影负责人分配工程师or区域工程师
	 * @param ipId 巡检计划id
	 * @param egId 工程师id
	 * @param egOrderBeginTime 预计服务开始时间
	 * @param egOrderEndTime 预计服务结束时间
	 * @param userId 用户表id
	 */
	public String tpAssignEg(String ipId, String egId, String egOrderBeginTime, String egOrderEndTime,String userId) {
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(egId) && StringUtil.isNotEmpty(egOrderBeginTime) && StringUtil.isNotEmpty(egOrderEndTime)){
				ZyInspectionPlanEntity zyInspectionPlan = systemService.getEntity(ZyInspectionPlanEntity.class, ipId);
				if(zyInspectionPlanService.judgeForBatchOperateMission(zyInspectionPlan,aj,userId)){
					ZyInspectionPlanEntity zyIp = new ZyInspectionPlanEntity();
					zyIp.setOrderBeginTime(DateUtils.str2Date(egOrderBeginTime, DateUtils.date_sdf));
					zyIp.setOrderEndTime(DateUtils.str2Date(egOrderEndTime, DateUtils.date_sdf));
					ZyEngineerEntity eg = systemService.getEntity(ZyEngineerEntity.class, egId);
					if(null != eg){
						zyIp.setEngineer(eg);
						aj = zyInspectionPlanService.doBatchAssignEngineer(ipId, zyIp);
					}
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-22 上午10:23:09
	 * Description WebService - 区域工程师拒绝任务 
	 * @param ipId 巡检计划id
	 * @param remark 拒绝原因
	 * @param userId 用户表id
	 */
	public String aegRefuseMission(String ipId,String remark,String userId){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(remark)){
				if(zyInspectionPlanService.judgeForAreaEngineerAssignEngineer(ipId,aj,userId)){
					zyInspectionPlanService.doBatchRefuseMissionOfAreaEngineer(ipId,remark,"");
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}

	/**
	 * @author xiul
	 * 2016-7-22 上午11:09:39
	 * Description WebService - 区域工程师分配任务 
	 * @param ipId 巡检计划id
	 * @param egId 工程师id
	 * @param egOrderBeginTime 预计服务开始时间
	 * @param egOrderEndTime 预计服务结束时间
	 * @param userId 用户表id
	 */
	public String aegAssignEg(String ipId, String egId, String egOrderBeginTime, String egOrderEndTime,String userId){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(egId) && StringUtil.isNotEmpty(egOrderBeginTime) && StringUtil.isNotEmpty(egOrderEndTime)){
				if(zyInspectionPlanService.judgeForAreaEngineerAssignEngineer(ipId,aj,userId)){
					zyInspectionPlanService.doBatchAreaAssignEngineer(ipId, egId ,egOrderBeginTime, egOrderEndTime);
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-22 下午2:46:57
	 * Description WebService - 工程师拒单
	 * @param ipId 巡检计划id
	 * @param remark 拒绝原因
	 * @param userId 用户表id
	 */
	public String egRefuseMisssion(String ipId, String remark,String userId){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(remark)){
				if(zyInspectionPlanService.judgeIpOfEgRefusePlan(ipId,null,aj,userId)){
					zyInspectionPlanService.doBatchRefuseMissionOfEngineer(ipId,remark,"");
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-22 下午2:46:57
	 * Description WebService - 工程师接单
	 * @param ipId 巡检计划id
	 * @param userId 用户表Id
	 */
	public String egAssignMisssion(String ipId,String userId){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId)){
				if(zyInspectionPlanService.judgeIpOfEgRefusePlan(ipId,null,aj,userId)){
					zyInspectionPlanService.doBatchEngineerAssignPlan(ipId);
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-22 下午3:01:00
	 * Description WebService - 工程师登记预约上门时间 
	 * @param ipId 巡检计划id
	 * @param engineerOrderTime 工程师预约上门时间
	 * @param engineerRemark 备注
	 */
	public String egUpdateOrderTime(String ipId,String engineerOrderTime,String engineerRemark){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(engineerOrderTime) && StringUtil.isNotEmpty(engineerRemark)){
				zyInspectionPlanService.doEngineerUpdateIp(ipId,engineerOrderTime,engineerRemark);
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-22 下午3:41:56
	 * Description WebService - 工程师填写巡检报告 
	 * @param ipId 巡检计划id
	 * @param userId 用户id
	 * @param jsIr 巡检报告json
	 */
	public String egAddIpReport(String ipId,String userId,String jsIr){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(ipId) && StringUtil.isNotEmpty(jsIr)){
				if(zyInspectionPlanService.judgePlanOfEgAddReport(ipId,aj,userId)){
					ZyInspectionReportEntity zyIr = JSON.parseObject(jsIr, ZyInspectionReportEntity.class);
					zyIr.setIsChecked(false);
					String irIpId = zyIr.getInspectionPlanId();
					List<ZyInspectionReportEntity> zyIrList = systemService.findByProperty(ZyInspectionReportEntity.class, "inspectionPlanId", irIpId);
					if(zyIrList.size() == 0){
						if(!StringUtil.isNotEmpty(ipId)){//巡检计划id不为空才可以保存巡检报告
							zyIr.setId(UUID.randomUUID().toString().replace("-", ""));
							systemService.save(zyIr);
							zyInspectionReportService.updateIpAndSavePlanRecord(zyIr);
						}else{
							aj.setSuccess(false);
						}
					}else{
						aj.setSuccess(false);
					}
				}else{
					aj.setSuccess(false);
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
	
	/**
	 * @author xiul
	 * 2016-7-22 下午4:59:13
	 * Description WebService - 设备变更申请 
	 * @param userType 申请人类型，1.工程师，2.第三方负责人
	 * @param userId 用户id
	 * @param changeType 变更类型
	 * @param planId 巡检计划id
	 * @param esSn 设备SN
	 * @param cmId 影院id
	 * @param reasonType 申请原因类型
	 * @param reason not required 申请原因 其它
	 */
	public String addEsChange(String userType, String userId, String changeType, String planId, String esSn, String cmId, String reasonType, String reason){
		Map<String, Object> rtMap = new HashMap<String, Object>();
		AjaxJson aj = new AjaxJson();
		try {
			if(StringUtil.isNotEmpty(userType) && StringUtil.isNotEmpty(userId) && StringUtil.isNotEmpty(changeType) && StringUtil.isNotEmpty(planId) && StringUtil.isNotEmpty(esSn)
					&& StringUtil.isNotEmpty(reasonType)){
				String company = null;
				TSBaseUser baseUser = null;
				if(userType.equals("1")){
					ZyEngineerEntity eg = systemService.getEntity(ZyEngineerEntity.class, userId);
					if(null != eg){
						baseUser = eg.getTsBaseUser();
					}
				}else if(userType.equals("2")){
					ZyContactsEntity ct = systemService.getEntity(ZyContactsEntity.class, userId);
					if(null != ct){
						baseUser = ct.getTsBaseUser();
					}
				}else{
					aj.setSuccess(false);
				}
				if(aj.isSuccess()){
					TSDepart depart = null;
					if(null != baseUser){
						List<TSUserOrg> tsUserOrgList = systemService.findHql("from TSUserOrg where tsUser.id = ? ", baseUser.getId());
						if(null != tsUserOrgList && tsUserOrgList.size() > 0){
							depart = tsUserOrgList.get(0).getTsDepart();
						}
						company = depart != null ? depart.getOrgCode() : company;
						ZyEquipmentChangeEntity esChange = new ZyEquipmentChangeEntity();
						esChange.setChangeType(changeType);
						esChange.setPlanId(planId);
						esChange.setEquipmentSpareSn(esSn);
						esChange.setZyCinema((ZyCinemaEntity)systemService.getEntity(ZyCinemaEntity.class, cmId));
						esChange.setReqReasonType(reasonType);
						esChange.setReqOtherReason(reason);
						esChange.setCreateBy(baseUser.getUserName());
						esChange.setCreateName(baseUser.getRealName());
						esChange.setCreateDate(new Date());
						String esChangeId = zyEquipmentChangeServiceI.doStartProcess(esChange, company);
						rtMap.put("esChangeId", esChangeId);
					}
				}
			}else{
				aj.setSuccess(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			aj.setSuccess(false);
		}
		rtMap.put("state", aj.isSuccess());
		return JSON.toJSONString(rtMap);
	}
}
